package com.hkts.naturalgas.base.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hkts.naturalgas.base.entity.*;
import com.hkts.naturalgas.base.entity.RoastingtwoPyjone;
import com.hkts.naturalgas.base.mapper.RoastingtwoPyjoneMapper;
import com.hkts.naturalgas.base.service.IRoastingtwoPyjoneService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkts.naturalgas.utils.CurveUtils;
import com.hkts.naturalgas.utils.DataHandlerUtils;
import com.hkts.naturalgas.utils.SnowFlakeUtil;
import com.hkts.naturalgas.utils.resultMapUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yck
 * @since 2021-06-25
 */
@Service
@Transactional
public class RoastingtwoPyjoneServiceImpl extends ServiceImpl<RoastingtwoPyjoneMapper, RoastingtwoPyjone> implements IRoastingtwoPyjoneService {


    @Autowired
    private RoastingtwoPyjoneMapper roastingtwoPyjoneMapper;

    @Override
    public IPage<RoastingtwoPyjone> selectPage(Page<RoastingtwoPyjone> page, RoastingtwoPyjone roastingtwoPyjone){
            QueryWrapper<RoastingtwoPyjone> query=Wrappers.query();
            QueryWrapper<RoastingtwoPyjone> entityWrapper=getEntityWrapper(query, roastingtwoPyjone);
            return roastingtwoPyjoneMapper.selectPage(page,entityWrapper);
    }


    @Override
    public List<RoastingtwoPyjone> selectList(RoastingtwoPyjone roastingtwoPyjone){
            QueryWrapper<RoastingtwoPyjone> query=Wrappers.query();
            QueryWrapper<RoastingtwoPyjone> entityWrapper=getEntityWrapper(query, roastingtwoPyjone);
            return roastingtwoPyjoneMapper.selectList(entityWrapper);
    }

    /**
     *  公共查询条件
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<RoastingtwoPyjone> getEntityWrapper(QueryWrapper<RoastingtwoPyjone> entityWrapper,RoastingtwoPyjone roastingtwoPyjone){
            //条件拼接

            return entityWrapper;
    }
    @Override
    public IPage<RoastingtwoPyjone> selectPageByTime(Page<RoastingtwoPyjone> page, String startDate, String endDate){
        QueryWrapper<RoastingtwoPyjone> query = Wrappers.query();
        query.lambda().apply(!"".equals(startDate),
                "time >= '" + startDate + "'")
                .apply(!"".equals(endDate),
                        "time <= '" + endDate + "'")
                .orderByDesc(RoastingtwoPyjone::getTime);
        IPage<RoastingtwoPyjone> hktsTailGasPage = roastingtwoPyjoneMapper.selectPage(page, query);
        return hktsTailGasPage;
    }
    @Override
    public IPage<RoastingtwoPyjone> getBrokenLineGraph(Page<RoastingtwoPyjone> page,String startDate, String endDate){
        QueryWrapper<RoastingtwoPyjone> query = Wrappers.query();
        query.lambda().apply(!"".equals(startDate),
                "time >= ' " + startDate + " ' " )
                .apply(!"".equals(endDate),
                        "time <= ' " + endDate + " ' ")
                .orderByAsc(RoastingtwoPyjone::getTime);
        IPage roastingoneRsoneRsjonePage = roastingtwoPyjoneMapper.selectPage(page,query);
        List<RoastingtwoPyjone> records = roastingoneRsoneRsjonePage.getRecords();
        List<Map<String,Object>> resultDate = records.stream().map(t -> assemblyCargoData(t)).collect(Collectors.toList());
        Map<String, List<Object>> stringListMap = resultMapUtils.resultData(resultDate);
        List<Object> list1 = resultMapUtils.getList(resultDate);
        List<Object> listName = resultMapUtils.getListName(resultDate);
        Map<String,Object> data = new HashMap<>();
        data.put("list",listName);
        data.put("records",stringListMap);
        data.put("times",list1);
        List<Object> resultList = new ArrayList<>();
        resultList.add(data);
        IPage iPage = roastingoneRsoneRsjonePage.setRecords(resultList);
        return iPage;
    }

    public Map<String,Object> assemblyCargoData(RoastingtwoPyjone vo){
        Map<String,Object> data = new LinkedHashMap<>();
        data.put("火道温度1(℃)",vo.getHdw1());
        data.put("火道温度2(℃)",vo.getHdw2());
        data.put("火道温度3(℃)",vo.getHdw3());
        data.put("火道温度4(℃)",vo.getHdw4());
        data.put("火道温度5(℃)",vo.getHdw5());
        data.put("火道温度6(℃)",vo.getHdw6());
        data.put("火道温度7(℃)",vo.getHdw7());
        data.put("火道温度8(℃)",vo.getHdw8());
        data.put("火道温度9(℃)",vo.getHdw9());
        data.put("火道温度10(℃)",vo.getHdw10());
        data.put("火道温度11(℃)",vo.getHdw11());
        data.put("火道温度12(℃)",vo.getHdw12());
        data.put("火道温度13(℃)",vo.getHdw13());
        data.put("火道温度14(℃)",vo.getHdw14());
        data.put("火道压力1",vo.getHdy1());
        data.put("火道压力2",vo.getHdy2());
        data.put("火道压力3",vo.getHdy3());
        data.put("火道压力4",vo.getHdy4());
        data.put("火道压力5",vo.getHdy5());
        data.put("火道压力6",vo.getHdy6());
        data.put("火道压力7",vo.getHdy7());
        data.put("火道压力8",vo.getHdy8());
        data.put("火道压力9",vo.getHdy9());
        data.put("火道压力10",vo.getHdy10());
        data.put("火道压力11",vo.getHdy11());
        data.put("火道压力12",vo.getHdy12());
        data.put("火道压力13",vo.getHdy13());
        data.put("火道压力14",vo.getHdy14());
        data.put("阀位1",vo.getFw1());
        data.put("阀位2",vo.getFw2());
        data.put("阀位3",vo.getFw3());
        data.put("阀位4",vo.getFw4());
        data.put("阀位5",vo.getFw5());
        data.put("阀位6",vo.getFw6());
        data.put("阀位7",vo.getFw7());
        data.put("阀位8",vo.getFw8());
        data.put("阀位9",vo.getFw9());
        data.put("阀位10",vo.getFw10());
        data.put("阀位11",vo.getFw11());
        data.put("阀位12",vo.getFw12());
        data.put("阀位13",vo.getFw13());
        data.put("阀位14",vo.getFw14());
        data.put("time",vo.getTime());
        return data;
    }

    @Override
    public void addHostoryData(String startDate, String endDate) throws ParseException {
        CurveUtils utils=new CurveUtils();
        utils.setStartDate(startDate);
        utils.getGrowthRate().add(9.0625f);
        utils.getTimeSlot().add(32);
        utils.setEndDate(endDate);
        utils.getData(260f);
        for(int i=0;i<utils.getDateList().size();i++){
            RoastingtwoPyjone one=new RoastingtwoPyjone(String.valueOf(SnowFlakeUtil.getFlowIdInstance().nextId()),
                    utils.getDateList().get(i),
                    DataHandlerUtils.getData(utils.getDataResult().get(i)),
                    DataHandlerUtils.getData(utils.getDataResult().get(i)),
                    DataHandlerUtils.getData(utils.getDataResult().get(i)),
                    DataHandlerUtils.getData(utils.getDataResult().get(i)),
                    DataHandlerUtils.getData(utils.getDataResult().get(i)),
                    DataHandlerUtils.getData(utils.getDataResult().get(i)),

                    DataHandlerUtils.getData(utils.getDataResult().get(i)),
                    DataHandlerUtils.getData(utils.getDataResult().get(i)),
                    DataHandlerUtils.getData(utils.getDataResult().get(i)),
                    DataHandlerUtils.getData(utils.getDataResult().get(i)),
                    DataHandlerUtils.getData(utils.getDataResult().get(i)),
                  0f,0f,0f,0f,0f,0f,0f,0f,0f,0f,0f,
                    DataHandlerUtils.getData(utils.getDataResult().get(i)),DataHandlerUtils.getData(utils.getDataResult().get(i)),DataHandlerUtils.getData(utils.getDataResult().get(i)),0f,0f,0f,0f,0f,0f,0f,
                    0f,0f,0f,0f,0f,0f,0f,0f,0f,0f
                    );

            roastingtwoPyjoneMapper.insert(one);
        }
    }

    @Override
    public void handlerPresserData(String endDate) {
        QueryWrapper<RoastingtwoPyjone> query=Wrappers.query();
        query.le("time",endDate);
        query.isNotNull("hdw1");
        List<RoastingtwoPyjone> roastingonePyjones = roastingtwoPyjoneMapper.selectList(query);
        for(int i=0;i<roastingonePyjones.size();i++){
            roastingonePyjones.get(i).setHdy1(DataHandlerUtils.getPressureRandomNumber());
            roastingonePyjones.get(i).setHdy2(DataHandlerUtils.getPressureRandomNumber());
            roastingonePyjones.get(i).setHdy3(DataHandlerUtils.getPressureRandomNumber());
            roastingonePyjones.get(i).setHdy4(DataHandlerUtils.getPressureRandomNumber());
            roastingonePyjones.get(i).setHdy5(DataHandlerUtils.getPressureRandomNumber());
            roastingonePyjones.get(i).setHdy6(DataHandlerUtils.getPressureRandomNumber());
            roastingonePyjones.get(i).setHdy7(DataHandlerUtils.getPressureRandomNumber());
            roastingonePyjones.get(i).setHdy8(DataHandlerUtils.getPressureRandomNumber());
            roastingonePyjones.get(i).setHdy9(DataHandlerUtils.getPressureRandomNumber());
            roastingonePyjones.get(i).setHdy10(DataHandlerUtils.getPressureRandomNumber());
            roastingonePyjones.get(i).setHdy11(DataHandlerUtils.getPressureRandomNumber());
            roastingonePyjones.get(i).setHdy12(DataHandlerUtils.getPressureRandomNumber());
            roastingonePyjones.get(i).setHdy13(DataHandlerUtils.getPressureRandomNumber());
            roastingonePyjones.get(i).setHdy14(DataHandlerUtils.getPressureRandomNumber());
        }
        this.saveOrUpdateBatch(roastingonePyjones);
    }

    @Override
    public void handlerCurveData(String startDate, String endDate, Float mbt, String order) {
        QueryWrapper<RoastingtwoPyjone> query=Wrappers.query();
        query.le("time",endDate);
        query.ge("time",startDate);
        List<RoastingtwoPyjone> roastingonePyjones = roastingtwoPyjoneMapper.selectList(query);
        List<Float> list=new ArrayList<Float>();
        if(order.equals("desc")){
            list=  DataHandlerUtils.getTemperatureCurve(mbt,roastingonePyjones.get(0).getHdw1(),roastingonePyjones.size(),order);
        }else{
            list=  DataHandlerUtils.getTemperatureCurve(mbt,roastingonePyjones.get(roastingonePyjones.size()-1).getHdw1(),roastingonePyjones.size(),order);
        }
        for(int i=0;i<roastingonePyjones.size();i++){
            roastingonePyjones.get(i).setHdw1( DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw2(DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw3( DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw4(DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw5( DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw6(DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw7( DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw8(DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw9( DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw10(DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw11( DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw12( DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw13( DataHandlerUtils.getData(list.get(i)));
            roastingonePyjones.get(i).setHdw14( DataHandlerUtils.getData(list.get(i)));
        }
        this.saveOrUpdateBatch(roastingonePyjones);
    }

    @Override
    public void handlerBugData() {
        QueryWrapper<RoastingtwoPyjone> query=Wrappers.query();
        query.lt("hdw12",0);
        List<RoastingtwoPyjone> roastingonePyjones = roastingtwoPyjoneMapper.selectList(query);
        Float hy1=0f;
        Float hy2=0f;
        Float hy3=0f;
        Float hw12=0f;
        Float hw13=0f;
        Float hw14=0f;
        for(int i=0;i<roastingonePyjones.size();i++){
            hw12=  roastingonePyjones.get(i).getHdw12();
            hw13=  roastingonePyjones.get(i).getHdw13();
            hw14=  roastingonePyjones.get(i).getHdw14();
            hy1=  roastingonePyjones.get(i).getHdy1();
            hy2=  roastingonePyjones.get(i).getHdy2();
            hy3=  roastingonePyjones.get(i).getHdy3();
            roastingonePyjones.get(i).setHdw12(hy1);
            roastingonePyjones.get(i).setHdw13(hy2);
            roastingonePyjones.get(i).setFw14(hy3);
            roastingonePyjones.get(i).setHdy1(hw12);
            roastingonePyjones.get(i).setHdy2(hw13);
            roastingonePyjones.get(i).setHdy3(hw14);
        }
        this.saveOrUpdateBatch(roastingonePyjones);
    }
}
